package com.achang.achangmall.product.service.impl;

import com.achang.achangmall.product.conf.ThreadPoolConfig;
import com.achang.achangmall.product.entity.SkuImagesEntity;
import com.achang.achangmall.product.entity.SpuInfoDescEntity;
import com.achang.achangmall.product.service.*;
import com.achang.achangmall.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.achang.common.utils.PageUtils;
import com.achang.common.utils.Query;

import com.achang.achangmall.product.dao.SkuInfoDao;
import com.achang.achangmall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and(item->{
                item.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            wrapper.eq("catelog_id",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)){
            wrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max) && !"0".equals(max)){
            wrapper.le("price",max);
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> infoEntityList = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return infoEntityList;
    }

    @Autowired
    private SkuImagesService imagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
//        SkuInfoEntity info = getById(skuId);
//        Long catalogId = info.getCatalogId();
//        Long spuId = info.getSpuId();
//
//        skuItemVo.setInfo(info);
//
//        List<SkuImagesEntity> images = imagesService.getImageBySkuId(skuId);
//        skuItemVo.setImages(images);
//
//        List<SkuItemVo.ItemSaleAttrVo>  saleAttrVos = skuSaleAttrValueService.getAttrsBySpuId(spuId);
//        skuItemVo.setSaleAttr(saleAttrVos);
//
//        SpuInfoDescEntity spuInfoDescServiceById = spuInfoDescService.getById(spuId);
//        skuItemVo.setDesp(spuInfoDescServiceById);
//
//        List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
//        skuItemVo.setGroupAttrs(attrGroupVos);



        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity infoEntity = getById(skuId);
            skuItemVo.setInfo(infoEntity);
            return infoEntity;
        }, executor);

        CompletableFuture<Void> acceptAsync = infoFuture.thenAcceptAsync(res -> {
            List<SkuImagesEntity> images = imagesService.getImageBySkuId(skuId);
            skuItemVo.setImages(images);
        });


        CompletableFuture<Void> voidCompletableFuture = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.ItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> completableFuture = infoFuture.thenAcceptAsync(res -> {
            SpuInfoDescEntity spuInfoDescServiceById = spuInfoDescService.getById(res.getSkuId());
            skuItemVo.setDesp(spuInfoDescServiceById);
        }, executor);

        CompletableFuture<Void> thenAcceptAsync = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        //等待所有任务都完成
        try {
            CompletableFuture.allOf(infoFuture,acceptAsync,voidCompletableFuture,completableFuture,thenAcceptAsync).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        return skuItemVo;
    }


}